package org.yxjme.ijkplayerlibrary.android;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.VideoView;

public class YXJVideoView extends FrameLayout {

    private VideoView videoView ;

    public YXJVideoView(@NonNull Context context) {
        this(context,null);
    }

    public YXJVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public YXJVideoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        createVideo();
    }
    /**
     * 添加呈现视频画面
     *
     * 当视频资源加载完成之后才显示
     */
    private void createVideo() {
        videoView = new VideoView(getContext());
        FrameLayout.LayoutParams f = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        f.gravity = Gravity.CENTER ;
        addView(videoView,f);
        addListener();
    }

    /**
     * 添加控制器
     *
     * @param yxjControlVideoView
     */
    public void addControlView(YXJControlVideoView yxjControlVideoView){
        if (yxjControlVideoView!=null){
            FrameLayout.LayoutParams f = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            f.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL ;
            addView(yxjControlVideoView,f);
            /**
             * 将视频播放器和控制界面关联起来
             */
            yxjControlVideoView.setYxjVideoVIew(videoView);
            /**
             * 添加视频回调接口
             */
            setYxjVideoListener(yxjControlVideoView);
        }
    }


    /**
     * 父级控件的宽度和高度
     *
     * @param changed
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        getWidth() ;
        getHeight() ;
    }


    /**
     * 视频类型
     *
     */
    public enum VideoType{
        FILEPATH ,
        URL
    }


    /**
     * 设置数据源
     *
     * @param path
     * @param videoType
     * @param <T>
     */
    public <T> YXJVideoView setDataPath(T path, VideoType videoType){
        switch (videoType){
            case FILEPATH:
                /*设置播放源*/
                videoView.setVideoPath((String) path);
                break;
            case URL:
                /*设置播放源*/
                videoView.setVideoURI(Uri.parse((String) path));
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + videoType);
        }
        return this;
    }



    private void addListener() {
        /*准备完成后回调*/
        videoView.setOnPreparedListener(preparedListener);
        /*播放内容监听*/
        videoView.setOnInfoListener(infoListener);
        /*播放完成回调*/
        videoView.setOnCompletionListener(completionListener);
    }


    private MediaPlayer.OnPreparedListener preparedListener = new MediaPlayer.OnPreparedListener(){

        @Override
        public void onPrepared(MediaPlayer mp) {
//            measureVideoSize(mp);
            if (yxjVideoListener!=null){
                yxjVideoListener.onPrepared(mp);
            }
        }
    };



    private MediaPlayer.OnInfoListener infoListener = new MediaPlayer.OnInfoListener(){

        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            if (yxjVideoListener!=null){
                yxjVideoListener.onInfo(mp,what,extra);
            }
            return false;
        }
    };

    private MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener(){

        @Override
        public void onCompletion(MediaPlayer mp) {
            if (yxjVideoListener!=null){
                yxjVideoListener.onCompletion(mp);
            }
        }
    };

    private YXJVideoListener yxjVideoListener;

    public void setYxjVideoListener(YXJVideoListener yxjVideoListener) {
        this.yxjVideoListener = yxjVideoListener;
    }
}
